using System;
using System.Collections.Generic;
using System.Drawing;
using DslCrossModelFramework.ModelTraversal;
using DslCrossModelFramework.UI;
using Microsoft.VisualStudio.Modeling;

namespace DslCrossModelFramework.NameResolution {
    public abstract class ModelElementNameResolver<TModelElementType> : IModelElementNameResolver where TModelElementType : ModelElement {

        public ModelElementNameResolver(){}

        public abstract string GetModelElementName(TModelElementType modelElement);

        public virtual string GetName(ModelElement modelElement) {
            if(modelElement == null) {
                return "<Unknown>";
            }
            if (modelElement is TModelElementType) {
                return this.GetModelElementName((TModelElementType)modelElement);
            }
            else {
                throw new ArgumentException(string.Format("Must be of type {0}", typeof(TModelElementType).FullName),"modelElement");
            }
        }

        public virtual string GetName(LinkedListNode<IModelPathSegment> segment) {
            return GetName(segment.Value.ModelElement);
        }

        public Type ModelElementType {
            get { return typeof (TModelElementType); }
        }

        public virtual ModelBrowserIcon GetIcon(ModelElement modelElement) {
            return new ModelBrowserIcon("dot",Image.FromStream(
                typeof (ModelElementTree).Assembly.GetManifestResourceStream(
                    string.Format("{0}.Resources.dot.bmp", typeof(ModelElementTree).Namespace))));
        }

        public virtual ModelBrowserIcon GetIcon(LinkedListNode<IModelPathSegment> segment) {
            return GetIcon(segment.Value.ModelElement);
        }
    }
}